package com.ruoyi.firefighter.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.award.service.IFirefighterAwardService;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.disease.service.IFirefighterDiseaseService;
import com.ruoyi.drill.service.IFirefighterDrillService;
import com.ruoyi.emergency.service.IFirefighterEmergencyService;
import com.ruoyi.firefighterFire.domain.FirefighterFire;
import com.ruoyi.firefighterFire.service.IFirefighterFireService;
import com.ruoyi.firefighterTrainingTable.service.IFirefighterTrainingTableService;
import com.ruoyi.graduation.service.IGraduationSchoolService;
import com.ruoyi.ifmEnlistIand.mapper.FirefighterFireMapper;
import com.ruoyi.ifmEnlistIand.mapper.SysDeptMapper;
import com.ruoyi.ifmEnlistIand.service.IFirefighterFireTableService;
import com.ruoyi.ifmEnlistIand.service.ISysDeptService;
import com.ruoyi.ifmEnlistIand.service.ISysUserService;
import com.ruoyi.ifmEnlistIand.service.impl.SysDeptServiceImpl;
import com.ruoyi.punishment.service.IFirefighterPunishmentService;
import com.ruoyi.qualification.service.IFirefighterQualificationService;
import com.ruoyi.record.service.IFirefighterRecordService;
import com.ruoyi.screeningDate.domain.Dcdate;
import com.ruoyi.security.service.IFirefighterSecurityService;
import com.ruoyi.subject.domain.FirefighterSubject;
import com.ruoyi.subject.mapper.FirefighterSubjectMapper;
import com.ruoyi.subject.service.IFirefighterSubjectService;
import com.ruoyi.task.service.IFirefighterFireTaskService;
import com.ruoyi.tournament.service.IFirefighterTournamentService;
import com.ruoyi.train.service.IFirefighterTrainService;
import com.ruoyi.vo.ExportFilter;
import com.ruoyi.vo.FirefighterVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.firefighter.mapper.FirefighterInfoMapper;
import com.ruoyi.firefighter.domain.FirefighterInfo;
import com.ruoyi.firefighter.service.IFirefighterInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 消防员信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-27
 */
@Service
@Transactional
public class FirefighterInfoServiceImpl implements IFirefighterInfoService
{
    @Autowired
    private FirefighterInfoMapper firefighterInfoMapper;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private IFirefighterRecordService firefighterRecordService;

    @Autowired
    private IFirefighterAwardService firefighterAwardService;

    @Autowired
    private IFirefighterDiseaseService firefighterDiseaseService;

    @Autowired
    private IFirefighterDrillService firefighterDrillService;

    @Autowired
    private IFirefighterSecurityService firefighterSecurityService;

    @Autowired
    private IFirefighterTournamentService firefighterTournamentService;

    @Autowired
    private IFirefighterEmergencyService firefighterEmergencyService;

    @Autowired
    private IFirefighterFireTaskService firefighterFireTaskService;

    @Autowired
    private IFirefighterQualificationService firefighterQualificationService;

    @Autowired
    private IFirefighterTrainService firefighterTrainService;

    @Autowired
    private IFirefighterPunishmentService firefighterPunishmentService;
    @Autowired
    private IGraduationSchoolService iGraduationSchoolService;
    /**
     * 查询消防员信息
     *
     * @param id 消防员信息主键
     * @return 消防员信息
     */
    @Override
    public FirefighterInfo selectFirefighterInfoById(Long id)
    {
        return firefighterInfoMapper.selectFirefighterInfoById(id);
    }

    @Override
    public FirefighterInfo selectFirefighterInfoByUserId(Long userId)
    {
        return firefighterInfoMapper.selectFirefighterInfoByUserId(userId);
    }

    /**
     * 查询消防员信息列表
     *
     * @param firefighterInfo 消防员信息
     * @return 消防员信息
     */
    @Override
    public List<FirefighterInfo> selectFirefighterInfoList(FirefighterInfo firefighterInfo)
    {
        return firefighterInfoMapper.selectFirefighterInfoList(firefighterInfo);
    }

    @Override
    public List<FirefighterInfo> selectFirefighterList(FirefighterInfo firefighterInfo)
    {
        return firefighterInfoMapper.selectFirefighterList(firefighterInfo);
    }

    /**
     * 新增消防员信息
     *
     * @param firefighterInfo 消防员信息
     * @return 结果
     */
    @Override
    public int insertFirefighterInfo(FirefighterInfo firefighterInfo)
    {
        SysUser selectBYuserID = sysUserService.selectUserById(firefighterInfo.getUserId());
        if (selectBYuserID !=null &&selectBYuserID.getDeptId()!=null){
            firefighterInfo.setDeptId(selectBYuserID.getDeptId());
            SysDept sysDept = sysDeptService.selectDeptById(selectBYuserID.getDeptId());
            firefighterInfo.setUnit(sysDept.getDeptName());
        }
        if (firefighterInfo.getUnit()!=null){
            SysDept sysDept = sysDeptService.selectSubDepartmentsByDeptName(firefighterInfo.getUnit());
            firefighterInfo.setDeptId(sysDept.getDeptId());
        }
        if (firefighterInfo.getUserId()==null){
            FirefighterInfo firefighterInfo1 = firefighterInfoMapper.selectFirefighterListMax();
            firefighterInfo.setUserId(firefighterInfo1.getUserId()+1);
        }
        return firefighterInfoMapper.insertFirefighterInfo(firefighterInfo);
    }

    /**
     * 修改消防员信息
     *
     * @param firefighterInfo 消防员信息
     * @return 结果
     */
    @Override
    public int updateFirefighterInfo(FirefighterInfo firefighterInfo)
    {
        return firefighterInfoMapper.updateFirefighterInfo(firefighterInfo);
    }

    /**
     * 批量删除消防员信息
     *
     * @param ids 需要删除的消防员信息主键
     * @return 结果
     */
    @Override
    public int deleteFirefighterInfoByIds(Long[] ids)
    {
        for (Long id : ids) {
            FirefighterInfo firefighterInfo = firefighterInfoMapper.selectFirefighterInfoById(id);
            firefighterRecordService.deleteFirefighterRecordById1(firefighterInfo.getUserId());
            firefighterQualificationService.deleteFirefighterQualificationById1(firefighterInfo.getUserId());
            firefighterAwardService.deleteFirefighterAwardById1(firefighterInfo.getUserId());
            firefighterPunishmentService.deleteFirefighterPunishmentById1(firefighterInfo.getUserId());
            firefighterTrainService.deleteFirefighterTrainById1(firefighterInfo.getUserId());
            firefighterFireTaskService.deleteFirefighterFireTaskById1(firefighterInfo.getUserId());
            firefighterEmergencyService.deleteFirefighterEmergencyById1(firefighterInfo.getUserId());
            firefighterTournamentService.deleteFirefighterTournamentById1(firefighterInfo.getUserId());
            firefighterSecurityService.deleteFirefighterSecurityById1(firefighterInfo.getUserId());
            firefighterDrillService.deleteFirefighterDrillById1(firefighterInfo.getUserId());
            firefighterDiseaseService.deleteFirefighterDiseaseById1(firefighterInfo.getUserId());
            iGraduationSchoolService.deleteGraduationSchoolById1(firefighterInfo.getUserId());
        }

        return firefighterInfoMapper.deleteFirefighterInfoByIds(ids);
    }

    /**
     * 删除消防员信息信息
     *
     * @param id 消防员信息主键
     * @return 结果
     */
    @Override
    public int deleteFirefighterInfoById(Long id)
    {

        return firefighterInfoMapper.deleteFirefighterInfoById(id);
    }






    @Autowired
    private FirefighterInfoMapper FirefighterInfoMapper;
    @Autowired
    private FirefighterSubjectMapper firefighterSubjectMapper;
    @Autowired
    private FirefighterFireMapper firefighterFireMapper;
    /**
     * 根据SysUser表中的'消防员证件号'(入参)  查询  在消防员信息表根据证件号码 查询 (消防员姓名 和 证件号码)-----------------SysUser表中的nickName===证件号码
     *
     * @param nickName
     */
    @Override
    public FirefighterVo selectFirefighterInfoByNickName(String nickName) {
        FirefighterVo firefighterVo = FirefighterInfoMapper.selectFirefighterInfoByNickName(nickName);
        return firefighterVo;
    }


    //查找條件
    @Override
    public List<FirefighterInfo> queryByFilter(ExportFilter filter) {
        // 根据filter对象的属性值构建SQL查询条件
        //通过id获取消防员的信息
        FirefighterInfo firefighterInfo = firefighterInfoMapper.selectFirefighterInfoById(filter.getFirefighterId());
        //通过id获取体能科目的信息
        FirefighterSubject firefighterSubject = firefighterSubjectMapper.selectFirefighterSubjectById(filter.getSubjectId());
        //  通过灭火救援岗位技能科目id获取信息
        FirefighterFire firefighterFire = firefighterFireMapper.selectFirefighterFireById(filter.getFireId());
        FirefighterInfo firefighterInfo1 = new FirefighterInfo();
        BeanUtils.copyProperties(firefighterInfo, firefighterInfo1);
        // 这里只是个简单的示例，实际应用中需要根据具体情况调整
        List<FirefighterInfo> result = new ArrayList<>();
        //信息放入集合并返回
        result.add(firefighterInfo1);
        // 查询逻辑
        return result;
    }

    @Override
    public FirefighterInfo selectFirefighterInfoByIdAndName(Dcdate dcdate) {
        return firefighterInfoMapper.selectFirefighterInfoByIdAndName(dcdate);
    }

    @Override
    public FirefighterInfo selectFirefighterInfoBySfz(FirefighterInfo firefighterIdCard) {
        return firefighterInfoMapper.selectFirefighterInfoBySfz(firefighterIdCard);
    }
}
